import socket  
from pynput.mouse import Controller, Button  
from pynput.keyboard import Controller as keyboardcontroller,Key
server_host='192.168.1.60'
def start_udp_server(host=server_host, port=12345):  
    mouse = Controller()  
    keyboard = keyboardcontroller()
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  
    server_socket.bind((host, port))  
    try:  
        while True:  
            data, _= server_socket.recvfrom(128) 
            datadecode=data.decode()
            if(datadecode[0]=='m'):
                splitdata=datadecode[2:].split(",")
                if(datadecode[1]=="m"):
                    mouse.position = (int(splitdata[0]), int(splitdata[1]))  
                elif(datadecode[1]=="1"):
                    if(splitdata[2]=="1"):
                        mouse.position = (int(splitdata[0]), int(splitdata[1]))  
                        mouse.press(Button.left)  
                    else:
                        mouse.release(Button.left)  
                elif(datadecode[1]=="s"):
                    mouse.scroll(int(splitdata[0]),int(splitdata[1]))   
                else:
                    if(splitdata[2]=="1"):
                        mouse.press(Button.right)  
                    else:
                        mouse.release(Button.right)  
            else:
                splitdata=datadecode[2:].split(",")
                if(datadecode[1]=="c"):
                    if(splitdata[1]=="1") and splitdata[0]!="None":
                        keyboard.press(splitdata[0])
                    elif (splitdata[0]!="None"):
                        keyboard.release(splitdata[0])
                else:
                    if(splitdata[1]=="1"):
                        if(splitdata[0]=="cq"):
                            keyboard.press(Key.ctrl)
                            keyboard.press('q')
                            keyboard.release('q')
                            keyboard.release(Key.ctrl)
                        elif(splitdata[0]=="cw"):
                            keyboard.press(Key.ctrl)
                            keyboard.press('w')
                            keyboard.release('w')
                            keyboard.release(Key.ctrl)
                        elif(splitdata[0]=="ce"):
                            keyboard.press(Key.ctrl)
                            keyboard.press('e')
                            keyboard.release('e')
                            keyboard.release(Key.ctrl)
                        elif(splitdata[0]=="cr"):
                            keyboard.press(Key.ctrl)
                            keyboard.press('r')
                            keyboard.release('r')
                            keyboard.release(Key.ctrl)
                        else:
                            keyboard.press(getattr(Key,splitdata[0][4:]))
                    else:
                        keyboard.release(getattr(Key,splitdata[0][4:]))
    except KeyboardInterrupt:  
        pass
    finally:  
        server_socket.close()  
start_udp_server()

